
import os
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
import S3DPath
from S3D import QtHelper, Sound
from S3D.Tools.Sound.EventSearcher.UI.SettingsDialog import Ui_SettingsDialog

EVENT_PROP_PROJECT      = 0
EVENT_PROP_PATH         = 1
EVENT_PROP_SOUNDBANK    = 2
EVENT_PROP_LEVELS       = 3
EVENT_PROP_SEARCH_PARAM = 4
EVENT_PROP_PARAM        = 5
EVENT_PROP_PARAMS_COUNT = len(Sound.EVENT_PROPERTIES)
EVENT_PROP_PARAM_LAST   = EVENT_PROP_PARAM + EVENT_PROP_PARAMS_COUNT - 1

EVENT_AUTOMATIC_PROPS = set([
    EVENT_PROP_SEARCH_PARAM
])

EVENT_PROP_DESC = [
    "Project",
    "Path",
    "Soundbank",
    "Levels",
    "<Auto: Search Parameter>",
]

EVENT_PROP_DESC.extend(desc for desc, _ in Sound.EVENT_PROPERTIES)

DEFAULT_EVENT_PROPS = (
    EVENT_PROP_PATH,
    EVENT_PROP_SOUNDBANK,
    EVENT_PROP_SEARCH_PARAM,
)

class SettingsDialog(QtGui.QDialog):
    def __init__(self, parent=None, lastProjects=None, lastParameters=None, lastSettingsTab=0):
        super(SettingsDialog, self).__init__(parent)

        self.ui = Ui_SettingsDialog()
        self.ui.setupUi(self)

        self.lastProjects   = lastProjects
        self.lastParameters = lastParameters if lastParameters is not None else DEFAULT_EVENT_PROPS
        self.lastSettingsTab = lastSettingsTab

        self.initUi()

    def initUi(self):
        self.ui.projectsPage.list   = self.ui.projectsList
        self.ui.parametersPage.list = self.ui.parametersList

        self.ui.toolBox.setCurrentIndex(self.lastSettingsTab)

        for path in Sound.getProjectsPathList():
            _, name = os.path.split(path)

            item = QtGui.QListWidgetItem(name)
            item.name = name
            item.path = path
            self.ui.projectsList.addItem(item)

            if self.lastProjects is None or name in self.lastProjects:
                item.setCheckState(Qt.Checked)
            else:
                item.setCheckState(Qt.Unchecked)

        for index, name in enumerate(EVENT_PROP_DESC):
            item = QtGui.QListWidgetItem(name)
            item.index = index
            self.ui.parametersList.addItem(item)

            if index in self.lastParameters:
                item.setCheckState(Qt.Checked)
            else:
                item.setCheckState(Qt.Unchecked)

    def onTabChanged(self, index):
        self.lastSettingsTab = index

    def onSelectAll(self):
        page = self.ui.toolBox.currentWidget()

        for item in QtHelper.items(page.list):
            item.setCheckState(Qt.Checked)

    def onSelectNone(self):
        page = self.ui.toolBox.currentWidget()

        for item in QtHelper.items(page.list):
            item.setCheckState(Qt.Unchecked)

    def onAccept(self):
        self.selectedProjects = projects = []

        for item in QtHelper.items(self.ui.projectsList):
            if item.checkState() == Qt.Checked:
                projects.append(item.name)

        self.selectedParameters = parameters = []
        for item in QtHelper.items(self.ui.parametersList):
            if item.checkState() == Qt.Checked:
                parameters.append(item.index)

        self.accept()

if __name__ == "__main__":
    from S3D.Util import Workspace
    Workspace.setWorkspace("Gravitas")

    dlg = SettingsDialog()
    if dlg.exec_() == QtGui.QDialog.Accepted:
        print(dlg.selectedProjects)
        print([EVENT_PROP_DESC[index] for index in dlg.selectedParameters])
